package logic

import (
	"context"
	"fmt"
	"jingdong-product/models/mysql"
	"jingdong-product/pkg"
	"jingdong-product/productrpc/internal/svc"
	"jingdong-product/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *productrpc.LoginRequest) (*productrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("account = ?", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在，禁止登录")
	}
	if pkg.Md5(in.Password) != user.Password {
		return nil, fmt.Errorf("密码错误，请重新输入")
	}
	return &productrpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
